<!-- components/Home.vue -->
<template>
    <div class="home">
        <h1>首页</h1>
        <div class="recommended-goods">
            <h2>推荐商品</h2>
            <div v-for="good in recommendedGoods" :key="good.id" class="good-card">
                <router-link :to="{ name: 'ProductDetail', params: { id: good.id } }">
                    <img :src="good.image" alt="商品图片" />
                    <div class="good-info">
                        <h3>{{ good.name }}</h3>
                        <p>价格：{{ good.price }}</p>
                    </div>
                </router-link>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import axios from 'axios';

const recommendedGoods = ref([]);

onMounted(() => {
    // 获取用户ID，这里假设从session中获取
    const userId = '123e4567-e89b-12d3-a456-426614174000';  // 替换为实际的用户ID获取逻辑

    fetchRecommendedGoods(userId);
});

const fetchRecommendedGoods = async (user_id) => {
    try {
        const response = await axios.get(`http://127.0.0.1:8000/app/recommend/${user_id}/`);
        recommendedGoods.value = response.data;
    } catch (error) {
        console.error('Error fetching recommended goods:', error);
    }
};
</script>

<style>
.home {
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;
    font-family: Arial, sans-serif;
    background-color: #f4f4f4;
    border-radius: 10px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

h1 {
    text-align: center;
    color: #333;
    margin-bottom: 20px;
    font-size: 2em;
}

.recommended-goods {
    display: flex;
    flex-wrap: wrap;
    gap: 20px;
}

.good-card {
    width: 200px;
    background-color: #fff;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    transition: transform 0.2s ease-in-out;
}

.good-card:hover {
    transform: scale(1.05);
}

.good-card img {
    width: 100%;
    height: 200px;
    object-fit: cover;
}

.good-info {
    padding: 10px;
}

.good-info h3 {
    font-size: 1.2em;
    margin: 0;
    color: #333;
}

.good-info p {
    margin: 5px 0;
    color: #555;
}
</style>